package cz.muni.fi.pa165.library.backend;

import cz.muni.fi.pa165.library.api.ReaderService;
import cz.muni.fi.pa165.library.api.ReaderTO;
import java.util.List;
import javax.jws.WebService;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author Matúš Abaffy
 */
@Service
@Transactional
@WebService(endpointInterface = "cz.muni.fi.pa165.library.api.ReaderService", serviceName = "ReaderWebService")
public class ReaderServiceImpl implements ReaderService {

    private ReaderDAO readerDAO;

    public void setReaderDAO(ReaderDAO readerDAO) {
        this.readerDAO = readerDAO;
    }

    @Secured("ROLE_LIBRARIAN")
    public ReaderTO createReader(ReaderTO reader) throws IllegalArgumentException {
        return DTOConvertor.convertReaderEntityToTO(readerDAO.createReader(DTOConvertor.convertReaderTOToEntity(reader)));
    }

    @Secured("ROLE_LIBRARIAN")
    public List<ReaderTO> findAllReaders() {
        return DTOConvertor.convertReaderEntityListToTOList(readerDAO.findAllReaders());
    }

    @Secured("ROLE_LIBRARIAN")
    public List<ReaderTO> findReaderByFull(String firstName, String surname, String email) {
        return DTOConvertor.convertReaderEntityListToTOList(readerDAO.findReaderByFull(firstName, surname, email));
    }
    
    @Secured("ROLE_LIBRARIAN")
    public ReaderTO findReaderById(Long id) throws IllegalArgumentException {
        return DTOConvertor.convertReaderEntityToTO(readerDAO.findReaderById(id));
    }

    @Secured("ROLE_LIBRARIAN")
    public List<ReaderTO> findReaderByName(String firstName, String surname) throws IllegalArgumentException {
        return DTOConvertor.convertReaderEntityListToTOList(readerDAO.findReaderByName(firstName, surname));
    }

    @Secured("ROLE_LIBRARIAN")
    public ReaderTO findReaderByBirthNumber(String birthNumber) throws IllegalArgumentException {
        return DTOConvertor.convertReaderEntityToTO(readerDAO.findReaderByBirthNumber(birthNumber));
    }

    @Secured("ROLE_LIBRARIAN")
    public ReaderTO findReaderByEmail(String email) throws IllegalArgumentException {
        return DTOConvertor.convertReaderEntityToTO(readerDAO.findReaderByEmail(email));
    }

    @Secured("ROLE_LIBRARIAN")
    public void updateReader(ReaderTO reader) throws IllegalArgumentException {
        readerDAO.updateReader(DTOConvertor.convertReaderTOToEntity(reader));
    }

    @Secured("ROLE_LIBRARIAN")
    public void deleteReader(ReaderTO reader) throws IllegalArgumentException {
        readerDAO.deleteReader(DTOConvertor.convertReaderTOToEntity(reader));
    }

    public ReaderTO authentize(String email, String password) {
        return DTOConvertor.convertReaderEntityToTO(readerDAO.authentize(email,password));
    }
}